Imports System.Data.Common


Partial Class Presentation_Control_AssignProject
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Sql As String
        Dim Records As DbDataReader
        Dim DefaultEntry As New ListItem

        Page.Title = "Assign or Remove from Project"

        If Not Page.IsPostBack Then

            'Populate the Executives List
            dlExecutives.DataTextField = "ExecutiveName"
            dlExecutives.DataValueField = "ExecutiveID"

            Sql = "SELECT ExecutiveID, ExecutiveName FROM ExecutiveMaster" & _
                " WHERE Active = 1 ORDER BY ExecutiveName"

            DefaultEntry.Text = "Select an Executive"
            DefaultEntry.Value = "0"
            DefaultEntry.Selected = True


            Records = GenHelper.ExecuteReader(Sql, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())
            dlExecutives.DataSource = Records
            dlExecutives.DataBind()
            dlExecutives.Items.Add(DefaultEntry)

            Records.Close()

            dlProjects.Visible = False
            Submit.Visible = False

        End If


    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
        Dim ExecutiveToAssignProject As New Executive(dlExecutives.SelectedValue)
        Dim Success As String

        Try
            Success = ExecutiveToAssignProject.AssignProject(dlProjects.SelectedValue, cbRemoveFromProject.Checked)


        Catch ex As InvalidOperationException

            lblSuccess.Text = ex.Message
            lblSuccess.Visible = True
            lblSuccess.ForeColor = Drawing.Color.Red
            Exit Sub

        End Try

        If cbRemoveFromProject.Checked = False Then

            lblSuccess.Text = "Successfully Assigned Project"
        Else
            lblSuccess.Text = "Successfully Removed from Project"

        End If


        lblSuccess.Visible = True
        lblSuccess.ForeColor = Drawing.Color.Black
        UpdateRoles()

    End Sub

    Protected Sub UpdateRoles()

        Dim Sql As String
        Dim Records As DbDataReader

        If dlExecutives.SelectedValue = 0 Then
            dlProjects.Visible = False
            Submit.Visible = False
            Exit Sub
        End If

        'Populate the Roles List
        dlProjects.DataTextField = "ProjectName"
        dlProjects.DataValueField = "ProjectID"

        If cbRemoveFromProject.Checked = False Then

            Sql = "SELECT ProjectID, ProjectName FROM ProjectMaster" & _
                " WHERE ProjectID NOT IN (Select DISTINCT ProjectID FROM ProjectAssignments WHERE ExecutiveID = " & dlExecutives.SelectedValue & ")" & _
                " AND Completed <> 1"

        Else

            Sql = "SELECT ProjectID, ProjectName FROM ProjectMaster" & _
                " WHERE ProjectID IN (Select DISTINCT ProjectID FROM ProjectAssignments WHERE ExecutiveID = " & dlExecutives.SelectedValue & ")" & _
                " AND Completed <> 1"

        End If



        Records = GenHelper.ExecuteReader(Sql, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())
        dlProjects.DataSource = Records
        dlProjects.DataBind()
        Records.Close()

    End Sub

    Protected Sub dlExecutives_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dlExecutives.SelectedIndexChanged

        'It's a post back. So populate the roles list
        dlProjects.Visible = True
        Submit.Visible = True
        UpdateRoles()

    End Sub

    Protected Sub cbRemoveFromProject_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbRemoveFromProject.CheckedChanged
        'It's a post back. So populate the roles list
        dlProjects.Visible = True
        Submit.Visible = True
        UpdateRoles()

    End Sub
End Class
